<template>
  <div class="mod-groupbuyingrecord">
    <el-form :inline="true" :model="searchForm" @keyup.enter.native="getDataList(1)">
      <el-form-item>
        <el-input v-model="searchForm.orderSn" placeholder="订单编号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchForm.status" placeholder="状态" clearable @change="getDataList(1)">
          <el-option
            key="0"
            label="拼团失败"
            value="0">
          </el-option>
          <el-option
            key="1"
            label="拼团中"
            value="1">
          </el-option>
          <el-option
            key="2"
            label="拼团成功"
            value="2">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchForm.isLeader" placeholder="是否团长" clearable @change="getDataList(1)">
          <el-option
            key="0"
            label="否"
            value="0">
          </el-option>
          <el-option
            key="1"
            label="是"
            value="1">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList(1)">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="goodsId"
        header-align="center"
        align="center"
        label="商品">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="showGoodsDetails(scope.row.goodsId)">{{scope.row.goodsName}}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        prop="userId"
        header-align="center"
        align="center"
        label="用户">
        <template slot-scope="scope">
          <el-img class="width120" v-model="scope.row.headImgUrl" disabled>
          </el-img>
          <el-button type="text" size="small" @click="showUserDetails(scope.row.userId)">{{scope.row.nickname}}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        prop="leaderNickname"
        header-align="center"
        align="center"
        label="团长">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="showUserDetails(scope.row.leaderId)">{{scope.row.leaderNickname}}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        prop="goodsDetail"
        header-align="center"
        align="center"
        label="商品规格">
      </el-table-column>
      <el-table-column
        prop="isLeader"
        header-align="center"
        align="center"
        label="是否团长">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isLeader===1"
            disabled>
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        prop="orderSn"
        header-align="center"
        align="center"
        label="订单编号">
      </el-table-column>
      <el-table-column
        prop="expireTime"
        header-align="center"
        align="center"
        label="到期时间">
      </el-table-column>
      <el-table-column
        prop="joinNumber"
        header-align="center"
        align="center"
        label="参加人数">
      </el-table-column>
      <el-table-column
        prop="groupNumber"
        header-align="center"
        align="center"
        label="成团人数">
      </el-table-column>
      <el-table-column
        prop="price"
        header-align="center"
        align="center"
        label="拼团价格">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="small" type="info">拼团失败</el-tag>
          <el-tag v-if="scope.row.status === 1" size="small" type="warning">拼团中</el-tag>
          <el-tag v-if="scope.row.status === 2" size="small" type="success">拼团成功</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="endTime"
        header-align="center"
        align="center"
        label="结束时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('mall:groupbuyingrecord:info')" type="text" size="small"
                     @click="showDetails(scope.row.id)">查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <user-detail v-if="userDetailVisible" ref="userDetail"></user-detail>
    <goods-detail v-if="goodsDetailVisible" ref="goodsDetail"></goods-detail>
  </div>
</template>

<script>
import AddOrUpdate from './groupbuyingrecord-add-or-update'
import UserDetail from './user-add-or-update'
import GoodsDetail from './goods-add-or-update'

export default {
  data () {
    return {
      userDetailVisible: false,
      goodsDetailVisible: false,
      searchForm: {
        orderSn: '',
        isLeader: '',
        status: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate,
    UserDetail,
    GoodsDetail
  },
  activated () {
    this.getDataList()
  },
  methods: {
    // 查看会员详情
    showUserDetails (id) {
      this.userDetailVisible = true
      this.$nextTick(() => {
        this.$refs.userDetail.init(id, true)
      })
    },
    // 查看商品详情
    showGoodsDetails (id) {
      this.goodsDetailVisible = true
      this.$nextTick(() => {
        this.$refs.goodsDetail.init(id, true)
      })
    },
    // 获取数据列表
    getDataList (page) {
      if (page) {
        this.pageIndex = page
      }
      this.$http({
        url: '/mall/groupbuyingrecord/list',
        method: 'get',
        params: {
          'page': this.pageIndex,
          'limit': this.pageSize,
          'orderSn': this.searchForm.orderSn,
          'isLeader': this.searchForm.isLeader || null,
          'status': this.searchForm.status || null
        }
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.records
          this.totalPage = data.page.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 查看详情
    showDetails (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, true)
      })
    }
  }
}
</script>
